/* eslint-disable require-atomic-updates */
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import { getRoutes } from '@/api/normal'
NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/overview', '/route'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  // const hasToken = getToken()
  // if (!store.backendRouterInit) { 路由守卫只会在路由跳转的时候处罚，刷新的时候不会触发
  //   const accessRoutes = await store.dispatch('backendRouter/generateRoutes', ['admin'])
  //   console.log('后端路由：', accessRoutes)
  //   // dynamically add accessible routes
  //   // 在使用addRoutes的地方
  //   // 重置当前router的match = 初始router.match
  //   router.options.routes.push(...accessRoutes)
  //   router.addRoutes(accessRoutes)
  //   store.backendRouterInit = true
  // }

  next()
  NProgress.done()
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
